package cn.com.selfsoft.smartcity.commons.util;

import cn.com.selfsoft.smartcity.commons.Constants;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.http.client.utils.URIBuilder;

import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class ParameterUtil {
    /**
     * request.getParameterMap 转Map<Stirng,Object>，忽略分页相关的参数
     *
     * @param parameterMap request.getParameterMap
     * @return
     */
    public static Map<String, Object> wrapObjectMapIgnorePage(Map<String, String[]> parameterMap) {
        return wrapObjectMap(parameterMap, Constants.DEFAULT_MARK_PARAMETER, Constants.DEFAULT_PAGE_PARAMETER, Constants.DEFAULT_CURRENT_PARAMETER, Constants.DEFAULT_SIZE_PARAMETER, Constants.DEFAULT_SORT_PARAMETER);
    }

    /**
     * request.getParameterMap 转Map<Stirng,Object>，忽略排序参数
     *
     * @param parameterMap request.getParameterMap
     * @return
     */
    public static Map<String, Object> wrapObjectMapIgnoreSort(Map<String, String[]> parameterMap) {
        return wrapObjectMap(parameterMap, Constants.DEFAULT_MARK_PARAMETER, Constants.DEFAULT_SORT_PARAMETER);
    }


    /**
     * request.getParameterMap 转Map<Stirng,Object>
     *
     * @param parameterMap     request.getParameterMap
     * @param ignoreProperties 需要忽略的属性数组
     * @return
     */
    public static Map<String, Object> wrapObjectMap(Map<String, String[]> parameterMap, String... ignoreProperties) {
        Map<String, Object> objectMap = new HashMap<>();
        parameterMap.forEach((key, value) -> {
            if (null != ignoreProperties && ArrayUtils.contains(ignoreProperties, key)) {
                return;
            } else {
                if (null != value) {
                    if("".equals(value[0])){
                        return;
                    }else if (value instanceof String[]) {
                        String[] strs = value;
                        if (1 < strs.length) {
                            List<String> list = new ArrayList<>();
                            for (String str : strs) {
                                list.add(str);
                            }
                            objectMap.put(key, list);
                        } else {
                            objectMap.put(key, strs[0]);
                        }
                    } else {
                        objectMap.put(key, value);
                    }
                } else {
                    objectMap.put(key, null);
                }
            }
        });
        return objectMap;
    }

    /***
     * 驼峰命名转为下划线命名
     *
     * @param parameterMap
     *        驼峰命名的字符串
     */

    public static Map<String, Object> HumpToUnderline(Map<String, String[]> parameterMap, String... ignoreProperties) {
        Map<String, Object> objectMap = new HashMap<>();
        parameterMap.forEach((oldkey, value) -> {
            String key = HumpToUnderline(oldkey);
            if (null != ignoreProperties && ArrayUtils.contains(ignoreProperties, oldkey)) {
                return;
            } else {
                if (null != value ) {
                    if("".equals(value[0])){
                        return;
                    }else if (value instanceof String[]) {
                        String[] strs = value;
                        if (1 < strs.length) {
                            List<String> list = new ArrayList<>();
                            for (String str : strs) {
                                list.add(str);
                            }
                            objectMap.put(key, list);
                        } else {
                            objectMap.put(key, strs[0]);
                        }
                    } else {
                        objectMap.put(key, value);
                    }
                } else {
                    objectMap.put(key, null);
                }
            }
        });
        return objectMap;
    }

    /***
     * 驼峰命名转为下划线命名
     *
     * @param para
     *        驼峰命名的字符串
     */

    public static String HumpToUnderline(String para) {
        StringBuilder sb = new StringBuilder(para);
        int temp = 0;//定位
        if (!para.contains("_")) {
            for (int i = 0; i < para.length(); i++) {
                if (Character.isUpperCase(para.charAt(i))) {
                    sb.replace(i,i+1,String.valueOf(Character.toLowerCase(para.charAt(i))));
                    sb.insert(i + temp, "_");
                    temp += 1;
                }
            }
        }
        return sb.toString();
    }

    public static String URIBuilderFromMap(String url,Map<String, Object> queryMap) throws URISyntaxException {
        // 创建uri
        URIBuilder builder = new URIBuilder(url);
        builder.clearParameters();
        if (queryMap != null) {
            for (String key : queryMap.keySet()) {
                builder.addParameter(key, queryMap.get(key).toString());
            }
        }
        URI uri = builder.build();

        return uri.toString();
    }



    public static void main(String[] args) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Map<String, String[]> stringMap = new HashMap<>();
        stringMap.put("1", new String[]{"1", "2", "3"});
        stringMap.put("2", new String[]{"f", "2s"});
        stringMap.put("3", new String[]{"f"});
        stringMap.put("3", new String[]{"f"});
        System.out.println(wrapObjectMap(stringMap, "3"));
    }
}
